package cn.edu.sig.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.stereotype.Repository;

import cn.edu.sig.bean.CurrentPage;
import cn.edu.sig.bean.Style;
import cn.edu.sig.bean.User;
import cn.edu.sig.bean.UserRole;
import cn.edu.sig.dao.UserDao;
import cn.edu.sig.util.PaginationHelper;

@Repository("userDao")
public class UserDaoImpl extends TemplateSupport implements UserDao {

	public void add(User t) {

		String sql = this.sqlSource.getSql("user.add");

		Object[] param = new Object[] { t.getCode(), t.getEmail(),
				t.getUserName()};

		this.getJdbcTemplate().update(sql, param);

	}

	public void delete(Integer id) {

	}

	public void edit(User t) {
		String sql = this.sqlSource.getSql("user.update");

		Object[] param = new Object[] { t.getUserName(), t.getSex(),
				t.getGrade(), t.getPhone(), t.getAddrezz(), t.getEmail(),
				t.getPhoto(), t.getCode() };

		this.getJdbcTemplate().update(sql, param);
	}

	public List<User> findAll() {
		String sql = this.sqlSource.getSql("user.list");

		RowMapper<User> mapper = new RowMapper<User>() {
			public User mapRow(ResultSet rs, int rowNum) throws SQLException {

				User user = new User();
				user.setCode(rs.getString("code"));
				user.setUserName(rs.getString("userName"));
				user.setEmail(rs.getString("email"));
				user.setGrade(rs.getString("grade"));

				return user;
			}
		};

		return this.getJdbcTemplate().query(sql, mapper);
	}

	public User findById(String id) {

		String sql = this.sqlSource.getSql("user.login");

		RowMapper<User> userMapper = new RowMapper<User>() {
			public User mapRow(ResultSet rs, int rowNum) throws SQLException {

				User user = new User();
				user.setCode(rs.getString("code"));
				user.setUserName(rs.getString("userName"));
				user.setEmail(rs.getString("email"));
				user.setAddrezz(rs.getString("addrezz"));
				user.setGrade(rs.getString("grade"));
				user.setSex(rs.getInt("sex"));
				user.setPhone(rs.getString("phone"));
				user.setPhoto(rs.getString("photo"));

				return user;
			}
		};

		return (User) this.getJdbcTemplate().queryForObject(sql,
				new Object[] { id }, userMapper);
	}

	public User findByName(String name) {
		return null;
	}

	public void register(User user) {
		String sql = this.sqlSource.getSql("user.register");

		Object[] param = new Object[] { user.getCode(), user.getSex(),
				user.getUserName()};

		this.getJdbcTemplate().update(sql, param);

	}

	public void updatePassword(String code, String password) {
		String sql = this.sqlSource.getSql("user.update.password");

		Object[] param = new Object[] { password, code };

		this.getJdbcTemplate().update(sql, param);

	}

	public CurrentPage<User> findUserList(int pageNo, int pageSize) {
		PaginationHelper<User> ph = new PaginationHelper<User>();
		return ph.fetchPage(this.getJdbcTemplate(),
				"SELECT count(*) FROM user", "SELECT * FROM user Limit ? ,?",
				"", pageNo, pageSize, new ParameterizedRowMapper<User>() {
					public User mapRow(ResultSet rs, int i) throws SQLException {
						User user = new User();
						user.setCode(rs.getString("code"));
						user.setUserName(rs.getString("userName"));
						user.setEmail(rs.getString("email"));
						user.setAddrezz(rs.getString("addrezz"));
						user.setGrade(rs.getString("grade"));
						user.setSex(rs.getInt("sex"));
						user.setPhone(rs.getString("phone"));

						return user;

					}
				});
	}

	public UserRole findUserRole(String name) {
		String sql = "select * from userrole where code = ?";

		
		Object[] param = new Object[] {name}; 
		RowMapper<UserRole> userMapper = new RowMapper<UserRole>() {
			public UserRole mapRow(ResultSet rs, int rowNum) throws SQLException {

				UserRole user = new UserRole();
				user.setCode(rs.getString("code"));
				user.setPassword(rs.getString("password"));
				user.setRole(rs.getString("role"));
				user.setEnable(rs.getBoolean("enable"));

				return user;
			}
		};

		return (UserRole) this.getJdbcTemplate().queryForObject(sql,
				param, userMapper);
	}

	public void setUserLock(boolean enable,User user) {
		String sql = "update userrole set enable=? where code = ?";
		Object[] param = new Object[] { enable, user.getCode() };

		this.getJdbcTemplate().update(sql, param);
		
	}

}
